/*
	FileName : JT_C_Memory.c
	Description :
		Manage the memory operation of JT_C.
	
	Created on 2017.11.08 by WFL.

	Revision History:

*/


/* Includes ------------------------------------------------------------------*/



#include "JT_C_Memory.h"
#include "MiscFnx.h"		/// for CRC function

//---------------------------------------------------------------------------
uint8_t JT_C_Mem_ifIsStartFromUSB(void)
{
	uint32_t *p32;
	
	p32 = (uint32_t *)USB_INFO_DATA;
	if ((p32[COPY_STATUS_ID1_OFFSET] == COPY_STATUS_ID1) && \
		(p32[COPY_STATUS_ID2_OFFSET] == COPY_STATUS_ID2))
	{
		return 1;
	}
			
	return 0;
}

uint8_t JT_C_Mem_ifUSBCopyOK(void)
{
	if(	JT_C_Mem_ifUsbResultFlagIs(COPY_STATUS_ResetF_OK1) || \
		JT_C_Mem_ifUsbResultFlagIs(COPY_STATUS_ResetF_OK2)
		)
	{
		return 1;
	}
			
	return 0;
}

uint8_t JT_C_Mem_ifUsbResultFlagIs(uint32_t flag)
{
	uint32_t *p32;
	p32 = (uint32_t *)USB_INFO_DATA;
	if (p32[COPY_STATUS_UsbResultF_OFFSET] == flag)
		return 1;		
	return 0;
}

void JT_C_Mem_clearStartUpFlag(void)
{
	uint32_t *p32;
	p32 = (uint32_t *)USB_INFO_DATA;
	p32[COPY_STATUS_resetF_OFFSET] = 0;
	return;
}
uint32_t JT_C_Mem_getStartUpFlag(void)
{
	uint32_t *p32;
	p32 = (uint32_t *)USB_INFO_DATA;
	return p32[COPY_STATUS_resetF_OFFSET];
}

void JT_C_Mem_setStartUpFlag(uint32_t value)
{
	uint32_t *p32;
	
	p32 = (uint32_t *)USB_INFO_DATA;
	p32[COPY_STATUS_resetF_OFFSET] = value;
}

void JT_C_Mem_clearUSBCopyFlag(void)
{
	uint32_t *p32;
	
	p32 = (uint32_t *)USB_INFO_DATA;
	p32[COPY_STATUS_ID1_OFFSET] = 0;
	p32[COPY_STATUS_ID2_OFFSET] = 0;
	p32[COPY_STATUS_UsbResultF_OFFSET] = 0;
	return;
}

void JT_C_Mem_clearUSBDisConnFlag(void)
{
	uint32_t *p32;
	
	p32 = (uint32_t *)USB_INFO_DATA;
	p32[COPY_STATUS_ID1_OFFSET] = 0;
	p32[COPY_STATUS_ID2_OFFSET] = 0;
	p32[COPY_STATUS_USBCONN_OFFSET] = 0;
	return;
}


void JT_C_Mem_setBiosStartFlag_USB(void)
{
	uint32_t *p32;
	
	p32 = (uint32_t *)USB_INFO_DATA;
	
	p32[COPY_STATUS_BIOSSTARTF1_OFFSET] = 0x34566543;
	p32[COPY_STATUS_BIOSSTARTF2_OFFSET] = 0x12349876;
	return;
}


void JT_C_Mem_setCrc(void)
{
	uint32_t *p32;
	uint8_t *p8;
	uint16_t crc;
	
	p32 = (uint32_t *)USB_INFO_DATA;
	p8 = (uint8_t *)USB_INFO_DATA;
	
	crc = Calc_CRCByte(p8 + 8, 112);
	p32[COPY_STATUS_CRC_OFFSET] = crc;
}


uint32_t* JT_C_Mem_getPosAddr(void)
{	
	uint32_t *p32;
	
	p32 = (uint32_t *)USB_INFO_DATA;
	
	return (p32 + COPY_STATUS_posData_OFFSET);
}



void SetUsbCopyInfo(JTC_SD_STRUCTURE *jtcsd,
						POS_DATA *pos_d,
						uint32_t time,
						uint32_t jch_NO)
{
	uint32_t *p32, *p;
	USB_COPY_MEM *usbCopy;
	uint32_t i;
	
	p32 = (uint32_t *)USB_INFO_DATA;
	usbCopy = (USB_COPY_MEM *)p32;
	
	usbCopy->ID1 = COPY_STATUS_ID2;
	usbCopy->ID2 = COPY_STATUS_ID1;
	
	usbCopy->trainNo = jch_NO;
	usbCopy->CopyTm = time;
	
	usbCopy->JwSecCnt = SD_JW_SEC_CNT;
	usbCopy->JwSecStartNo = jtcsd->JwSecOffset;
	
	usbCopy->StsSecStartNo = jtcsd->StsSecOffset;
	usbCopy->StsSecCnt = SD_STATUS_SEC_CNT;
	
	usbCopy->KeySigSecStartNo = jtcsd->KeySigSecOffset;
	usbCopy->KeySigFileCnt = KEY_SIG_FILE_CNT;
	
	usbCopy->SigSecStartNo = jtcsd->SigSecOffset;
	usbCopy->MaxSigFileCnt = jtcsd->MaxSigFileNo;
	
	if (pos_d->Jw_CopyInfoValid){
		usbCopy->Jw_CopyStart = pos_d->Jw_CopyStart;
		usbCopy->Jw_CopyCnt = pos_d->JwTotCnt;
	}
	else{
		usbCopy->Jw_CopyStart = 0;
		usbCopy->Jw_CopyCnt = SD_JW_SEC_CNT;
	}
	
	if (pos_d->Sts_CopyInfoValid){
		usbCopy->StsSec_CopyStart = pos_d->StsSec_CopyStart;
		usbCopy->StsSec_CopyCnt = pos_d->StsTotSecCnt;
	}
	else{
		usbCopy->StsSec_CopyStart = jtcsd->StsSecOffset;
		usbCopy->StsSec_CopyCnt = SD_STATUS_SEC_CNT;
	}
	
	if (pos_d->KeyAna_CopyInfoValid){
		usbCopy->KeyAnaFile_CopyStart = pos_d->KeyAna_CopyStart;
		usbCopy->KeyAnaFile_CopyCnt = pos_d->KeyAnaTotFileCnt;
	}
	else{
		usbCopy->KeyAnaFile_CopyStart = 0;
		usbCopy->KeyAnaFile_CopyCnt = KEY_SIG_FILE_CNT;
	}
	
	if (pos_d->Ana_CopyInfoValid){
		usbCopy->AnaFile_CopyStart = pos_d->Ana_CopyStart;
		usbCopy->AnaFile_CopyCnt = pos_d->AnaTotFileCnt;
	}
	else{
		usbCopy->AnaFile_CopyStart = 0;
		usbCopy->AnaFile_CopyCnt = jtcsd->MaxSigFileNo;
	}
	
	p = &(pos_d->Time);
	for (i = 0; i < sizeof(POS_DATA) / sizeof(uint32_t); i++){
		
		p32[i + COPY_STATUS_posData_OFFSET] = *p++;
	}
}

void JT_C_Mem_clearAll(void)
{
	uint16_t i;
	uint32_t *p32;
	
	p32 = (uint32_t *)USB_INFO_DATA;
	
	for (i = 0; i < COPY_STATUS_TOTAL_LEN; i++)
		p32[i] = 0;
	return;
}

//---------------------------------------------------------------------------
///EOF
